package com.itheima.web;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.HotSetmeal;
import com.itheima.pojo.ReportData;
import com.itheima.service.ReportService;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        DateTime now = new DateTime();//当前时间

        List<String> months = new ArrayList<>();

        DateTime dateTime = DateUtil.offsetMonth(now, -12);//2019-04

        for (int i = 0; i <= 12; i++) {
            DateTime time = DateUtil.offsetMonth(dateTime, i);//往后每次偏移i个月
            months.add(time.toString("yyyy-MM"));
        }

        List<Integer> memberCount = reportService.getMemberReport(months);

        Map map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return Result.OK("",map);
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
       List<Map> maps =  reportService.getSetmealReport();
       return Result.OK("",maps);
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
       ReportData reportData = reportService.getBusinessReportData();
       return Result.OK("",reportData);
    }

    @RequestMapping("/exportBusinessReport")
    public void exportBusinessReport(HttpServletResponse response, HttpServletRequest request){
        try {
            ReportData reportData = reportService.getBusinessReportData();
            //1、加载Excel模板
            String realPath = request.getSession().getServletContext().getRealPath("template");
            String templatePath = realPath + "/report_template.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook(templatePath);
                //2、写Excel
                XSSFSheet sheetAt = workbook.getSheetAt(0);

                sheetAt.getRow(2).getCell(5).setCellValue(reportData.getReportDate());
                sheetAt.getRow(4).getCell(5).setCellValue(reportData.getTodayNewMember());
                sheetAt.getRow(4).getCell(7).setCellValue(reportData.getTotalMember());

                sheetAt.getRow(5).getCell(5).setCellValue(reportData.getThisWeekNewMember());
                sheetAt.getRow(5).getCell(7).setCellValue(reportData.getThisMonthNewMember());

                sheetAt.getRow(7).getCell(5).setCellValue(reportData.getTodayOrderNumber());
                sheetAt.getRow(7).getCell(7).setCellValue(reportData.getTodayVisitsNumber());

                sheetAt.getRow(8).getCell(5).setCellValue(reportData.getThisWeekOrderNumber());
                sheetAt.getRow(8).getCell(7).setCellValue(reportData.getThisWeekVisitsNumber());

                sheetAt.getRow(9).getCell(5).setCellValue(reportData.getThisMonthOrderNumber());
                sheetAt.getRow(9).getCell(7).setCellValue(reportData.getThisMonthVisitsNumber());

                int i = 12;
                List<HotSetmeal> hotSetmeal = reportData.getHotSetmeal();
                for (HotSetmeal setmeal : hotSetmeal) {
                    sheetAt.getRow(i).getCell(4).setCellValue(setmeal.getName());
                    sheetAt.getRow(i).getCell(5).setCellValue(setmeal.getSetmealCount());
                    sheetAt.getRow(i).getCell(6).setCellValue(setmeal.getProportion().doubleValue());
                    i ++;
            }
            //3、把Excel通过response写回到浏览器
            ServletOutputStream out = response.getOutputStream();
                //告诉浏览器现在返回的是excel
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition",
                    "attachment;filename="+ java.net.URLEncoder.encode("运营数据报表", "UTF-8")+".xlsx");
            workbook.write(out);

            out.flush();
            out.close();
            workbook.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/getMemberByAgeAndGender")
    public List getMemberByAgeAndGender(){
        List<List> lists = reportService.getMemberByAgeAndGender();
        return lists;
    }

    /*public static void main(String[] args) {
//        1、过去一年时间（现在往偏移12个月，然后把这个时间之间的数据按月统计）
        DateTime now = new DateTime();

        List<String> months = new ArrayList<>();

        DateTime dateTime = DateUtil.offsetMonth(now, -12);//2019-04

        for (int i = 0; i < 12; i++) {
            DateTime time = DateUtil.offsetMonth(dateTime, i);//往后每次偏移i个月
            months.add(time.toString("yyyy-MM"));
        }

        System.out.println(months);

    }*/
}
